package com.zzz.web.Servlet;

import com.zzz.web.DB.operationOnDataBase;
import com.zzz.web.Data.RSSI;
import com.zzz.web.LocationAlgo.KalmanFilter;
import com.zzz.web.tools.tool;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class GatherData extends HttpServlet {
    private int lastNumber = 0;
    private int currentNumber = 0;
    private ArrayList<RSSI> rssiList = new ArrayList<RSSI>();
    private StringBuffer tempstring = new StringBuffer();
    private StringBuffer stringBuffer = new StringBuffer();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String data = req.getParameter("data");

        RSSI rssi = tool.getRssi(data);
        currentNumber = rssi.getNumber();
        if(currentNumber != lastNumber  ){
            rssiList.clear();
            lastNumber = currentNumber;
        }
        System.out.println(rssi.toString());
        rssiList.add(rssi);
        if (rssiList.size() >= 10) {
            new Thread() {
                @Override
                public void run() {
                    ArrayList<RSSI> temprssiList = new ArrayList<RSSI>(rssiList);
                    rssiList.clear();
                    new operationOnDataBase().insertIntoData(new KalmanFilter().Run(temprssiList) );
                }
            }.start();
            lastNumber = currentNumber;
            tempstring.append("插入成功");
        }
    }


}
//http://120.27.237.68:8080/wifilocation/gatherData?data=number=13245,frequency=0,AP1MAC=a0:57:e3:c0:f2:71,AP1RSSI=1,AP2MAC=a0:57:e3:c0:f2:71,AP2RSSI=2,AP3MAC=a0:57:e3:c0:f2:71,AP3RSSI=3